package qldc.pm.tiensu.searchcontainer;

import java.text.SimpleDateFormat;

import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;

import qldc.pm.tiensu.searchcontainer.util.nguoicanxoaantich.YeuCauXoaAnTichComparatorUtil;

import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.PortalPreferences;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

public class YeuCauXoaAnTichSearch extends SearchContainer<YeuCauXoaAnTichSearch> {
	private static Log _log = LogFactoryUtil.getLog(YeuCauXoaAnTichSearch.class);

	public static final String EMPTY_RESULTS_MESSAGE = "khong-tim-thay-yeu-cau-xoa-an-tich-nao";

	public YeuCauXoaAnTichSearch(RenderRequest renderRequest, PortletURL iteratorURL) {
		super(renderRequest, new YeuCauXoaAnTichDisplayTerms(renderRequest),
				new YeuCauXoaAnTichSearchTerms(renderRequest), DEFAULT_CUR_PARAM,
				DEFAULT_DELTA, iteratorURL, null, EMPTY_RESULTS_MESSAGE);

		YeuCauXoaAnTichDisplayTerms displayTerms = (YeuCauXoaAnTichDisplayTerms) getDisplayTerms();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

		iteratorURL.setParameter(YeuCauXoaAnTichSearchTerms.HO_VA_TEN_NGUOI_YEU_CAU_XOA_AN_TICH , displayTerms.getHoVaTenNguoiYeuCauXoaAnTich());
		iteratorURL.setParameter(YeuCauXoaAnTichSearchTerms.MA_SO_HO_SO_XOA_AN_TICH , displayTerms.getMaSoHoSoXoaAnTich());
		iteratorURL.setParameter(YeuCauXoaAnTichSearchTerms.TRANG_THAI_XU_LY_HO_SO_XOA_AN_TICH ,StringUtil.valueOf(displayTerms.getTrangThaiXuLyHoSoXoaAnTich()));


		if (displayTerms.getNgayLapHoSoXoaAnTich() != null) {
			iteratorURL.setParameter(YeuCauXoaAnTichSearchTerms.NGAY_LAP_HO_SO_XOA_AN_TICH,
					dateFormat.format(displayTerms.getNgayLapHoSoXoaAnTich()));
		}

		try {
			PortalPreferences preferences = PortletPreferencesFactoryUtil.getPortalPreferences(renderRequest);

			String orderByCol = ParamUtil.getString(renderRequest, "orderByCol");
			String orderByType = ParamUtil.getString(renderRequest,"orderByType");
			String portletName = "tiensu";

			if(Validator.isNotNull(orderByCol) && Validator.isNotNull(orderByType)){
				preferences.setValue(portletName, "order-by-col", orderByCol);
				preferences.setValue(portletName, "order-by-type", orderByType);

			} else{
				orderByCol = preferences.getValue(portletName, "order-by-col", "");
				orderByType = preferences.getValue(portletName, "order-by-type", "asc");
			}

			_log.info("orderByCol================= " + orderByCol);

			OrderByComparator orderByComparator = YeuCauXoaAnTichComparatorUtil.getYeuCauXoaAnTichComparator(orderByCol, orderByType);

			setOrderByCol(orderByCol);
			setOrderByType(orderByType);
			setOrderByComparator(orderByComparator);
		} catch (SystemException e) {
			_log.warn(e);
		}
	}
}
